<template>
	<jk-dialog
		v-model="showDialog"
		title="选择-品种"
		width="1000px"
		:confirm-btn-loading="confirmBtnLoading"
		@on-visible-change="onVisibleChange"
		@on-cancel="onVisibleChange(false)"
		@on-confirm="onConfirmBtn"
	>
		<header ref="box1" class="margin-bottom-4">
			<!--<div class="flex-center margin-bottom-10">
				<el-radio-group v-model="searchParams.openingState" size="small" @change="onOpeningStateChange">
					<el-radio-button v-for="item in openStateList" :key="item.id" :label="item.id">
						<div class="margin-right-10">{{ item.name }}</div>
					</el-radio-button>
				</el-radio-group>
			</div>-->
			<div class="flex-between-center">
				<div></div>
				<!--<jk-select v-model="searchParams.workshopId" :option-list="workshopList" clearable placeholder="区域" size="small" class="search-bar-item-size2 item-gap"></jk-select>-->
				<div class="flex-start-center">
					<jk-search-input
						v-model="searchParams.processName"
						disabled
						class="search-bar-item-size1 item-gap"
						placeholder="工序"
					></jk-search-input>
					<jk-search-input
						v-model="searchParams.productNameOrCode"
						class="search-bar-item-size3 item-gap"
						placeholder="品种编号或名称"
						@on-search="onSearchBtn"
					></jk-search-input>
					<jk-search-input
						v-model="searchParams.machineNameOrCode"
						class="search-bar-item-size2 item-gap"
						placeholder="机台编号或名称"
						@on-search="onSearchBtn"
					></jk-search-input>
					<!--<jk-select
						v-model="searchParams.outputTubeColorName"
						value-key-name="name"
						:option-list="xsColorList"
						class="search-bar-item-size2 item-gap"
						placeholder="细纱管颜色"
						@on-search="onSearchBtn"
					></jk-select>-->
					<jk-select
						v-model="searchParams.paperTubeColorName"
						value-key-name="name"
						:option-list="ltColorList"
						class="search-bar-item-size2 item-gap"
						placeholder="纸管颜色"
						@on-search="onSearchBtn"
					></jk-select>
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</header>

		<jk-table
			ref="jkTable"
			:loading="tableLoading"
			class="width-100-percent"
			:data="tableData"
			border
			highlight-hover-row
			highlight-current-row
			:height="dialogTableHeight - 10"
			@radio-change="onRadioChange"
			@cell-dblclick="onCellDblclick"
		>
			<vxe-table-column type="radio" width="60" align="center" title="单选" fixed="left"></vxe-table-column>
			<!--<vxe-column title="机台" align="left" field="machineName" :width="90" />-->
			<vxe-column title="品种" align="left" field="productName" :min-width="100" />
			<!--<vxe-column title="批号" align="left" field="batchCode" :min-width="90" />-->
			<!--<vxe-column title="细纱管颜色" align="left" field="outputTubeColorList" :width="90">
				<template slot-scope="scope">
					<span>{{ filterColor(scope.row.outputTubeColorList || []) }}</span>
				</template>
			</vxe-column>-->
			<vxe-column title="纸管颜色" align="left" field="outputTubeColorList" :width="90">
				<template slot-scope="scope">
					<span>{{ filterColor(scope.row.paperTubeList || []) }}</span>
				</template>
			</vxe-column>
			<vxe-column title="翻改日期" align="left" field="changeDate" :width="110" />
			<vxe-column title="生产订单" align="left" field="productOrderCode" :width="150" />
			<vxe-column title="工序" align="left" field="processName" :width="70" />
			<vxe-column title="区域" align="left" field="workshopName" :width="80" />
		</jk-table>
		<footer ref="box2" class="flex-end-center margin-top-10">
			<jk-page
				:page-index="searchParams.pageNo"
				:page-sizes="searchParams.pageSizes"
				:page-size="searchParams.pageSize"
				:total="totalCount"
				@on-change="onPageChange"
			/>
		</footer>
	</jk-dialog>
</template>

<script>
    import calDialogHeight from '../../../../mixins/cal-dialog-height';
    import { pageConfig } from '../../../../utils/jk/jk-tool';
    import {
        productionPrdNoticeMachineChangeGetByColor,
        productionPrdNoticeMachineChangePostPage
    } from '../../../../api/production/xsRenovateOrder';
    import { openStateList } from '../../../../utils/jk/status-option-list';
    import filterColor from '../../order/components/form-box/filter-color';

    export default {
        mixins: [calDialogHeight, filterColor],
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            formData: {
                type: Object,
                default: () => {}
            }
        },
        data() {
            return {
                xsColorList: [],
                ltColorList: [],
                openStateList: openStateList,
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    processCode: '',
                    outputTubeColorName: '',
                    paperTubeColorName: '',
                    productNameOrCode: ''
                },
                confirmBtnLoading: false,
                totalCount: 0,
                workshopList: [],
                processList: [],
                selectRow: null,
                tableData: [],
                showDialog: false,
                tableLoading: false
            };
        },
        methods: {
            onOpeningStateChange() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onCellDblclick(e) {
                this.$emit('on-confirm', e.row);
            },
            onRadioChange(selection) {
                this.selectRow = selection.row;
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            onConfirmBtn() {
                if (this.selectRow) {
                    this.$emit('on-confirm', JSON.parse(JSON.stringify(this.selectRow)));
                } else {
                    this.$tipsBar(this, 'custom', 'warning', '请选择数据');
                }
            },
            getListRequest() {
                this.tableLoading = true;
                const params = JSON.parse(JSON.stringify(this.searchParams));
                return productionPrdNoticeMachineChangeGetByColor(params).then(res => {
                    // return productionPrdNoticeMachineChangePostPage(params).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.totalCount = res.data.count;
                        this.tableData = res.data.res || [];
                        this.getXsAndLtColorList();
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    this.confirmBtnLoading = false;
                    Object.assign(this.$data, this.$options.data());
                }
            },
            // 获取细纱和络筒颜色的下拉选项
            getXsAndLtColorList() {
                this.xsColorList = [];
                this.ltColorList = [];
                this.tableData.forEach(x => {
                    if (x.outputTubeColorList) {
                        x.outputTubeColorList.forEach(colorItem => {
                            // 只取启用的并做去重处理
                            if (colorItem.auditState && !this.xsColorList.some(x => x.name === colorItem.name)) {
                                this.xsColorList.push(colorItem);
                            }
                        });
                    }
                    if (x.paperTubeList) {
                        x.paperTubeList.forEach(colorItem => {
                            // 只取启用的并做去重处理
                            if (colorItem.auditState && !this.ltColorList.some(x => x.name === colorItem.name)) {
                                this.ltColorList.push(colorItem);
                            }
                        });
                    }
                });
            },
            async getDependentData() {
                this.tableLoading = true;
                this.workshopList = this.$store.state.user.userWorkshop.workshopList;
                this.processList = this.$store.state.user.userProcess.processList;
                await this.getListRequest();
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    if (this.formData.processCode === 'LT') {
                        this.$set(this.searchParams, 'processCode', 'XS');
                        this.$set(this.searchParams, 'processName', '细纱');
                    } else {
                        this.$set(this.searchParams, 'processCode', this.formData.processCode);
                        this.$set(this.searchParams, 'processName', this.formData.processName);
                        this.$set(this.searchParams, 'machineNameOrCode', this.formData.machineName);
                    }
                    this.getDependentData();
                }
            }
        }
    };
</script>
